import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.JFreeChartPanel;


public class MarkovApplet implements ActionListener, Runnable
{
	
	public static  int a;
	public int var;
	public  int b;
	public int e;
	 MarkovQueue queue;               
	  QueueStateDataset stateEvolData; 
	   HistDataset stateFreqData;
	   HistDataset arrivalProbData;     
	    RealHistDataset serviceProbData;
	    
	    Thread thread = null;            
	    protected boolean done = true;   

	    ParamDialog paramDialog; 
   
	  public int serverValue = 1;
	  public double tickValue = 0.1;
	  public double lambdaValue = 1;
	  public double muValue = 1;
   	
	public Font f = new Font("Times new roman" , Font.BOLD , 18);	
	public JFrame jf;
	public Container c;
	 
	
	MarkovApplet()
	{

		jf = new JFrame("Graphical");
		c = jf.getContentPane();
		c.setLayout(null);
		jf.setSize(800,670);
		
		c.setBackground(Color.GREEN);
			
		JPanel statusPanel = new JPanel();
		statusPanel.setLayout(new GridLayout(2, 4, 1, 1));
	
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		
		JPanel chartPanel1 = new JPanel();
	  
		chartPanel1.setLayout(new BoxLayout(chartPanel1, BoxLayout.X_AXIS));

		
		stateEvolData = new QueueStateDataset();
		JFreeChart stateEvolChart = 
		    ChartFactory.createXYChart("Bandwidth Evaluation", "File size(bits)", 
					       "Bandwidth", stateEvolData, false);
		stateEvolChart.DEFAULT_BACKGROUND_COLOR=Color.GREEN;
				
		JFreeChartPanel stateEvolChartPanel = 
		    new JFreeChartPanel(stateEvolChart, 300, 300, false, 300, 300);
		
		chartPanel1.add(stateEvolChartPanel);
		
		chartPanel1.add(Box.createRigidArea(new Dimension(15, 0)));
        		
		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
		c.add(chartPanel1);
		
		jf.show();
	jf.setSize(1400,740);
		
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent win) {
				System.exit(0);
			}
		});
		if(a!=0){
			
		             	b=a/1024;
		             	
		             	
		             	System.out.println("A value"+var);
			       //  if(var<1)
			        // {
				//
				  //  b=1;
			     // }
			
		if (done) {
			

			done = false;
			queue = new MarkovQueue(serverValue, tickValue, 
						lambdaValue, muValue);
			thread = new Thread(this);
			thread.start();

			System.out.println("Simulation started");
		    } 
	    
		    start();
		}
	}
	 @SuppressWarnings("deprecation")
	public void start() {
			if (thread != null) {
			    thread.resume();

			    

			    System.out.println("Simulation resumed");
			} 
		    }
	
	    public void updateStatusFields(double t, int s, int a, int d) {
		String str = String.valueOf(t);
		
		if (str != null) {
		    if (str.length() > 4) {
			str = str.substring(0, 4);
		    }
		} else {
		    str = new String("0");
		}	 
		
	    }
	   
	   	    
		public void run() {
			
			//if(b==0)
			//{
				
			//}
			
			//else
			
			if(b<1)
			{
				e=0;
				
				b=2;
				while(!done)
				{
					for(int i=0;i<1;i++)
					{
			  
						queue.simulateStep();
					}
		
				    double t = queue.getTime();
					int s = queue.getState();
					int a = queue.getLastArrivals();
					int d = queue.getLastDepartures();
				  
					stateEvolData.add(t*1000, s);
					updateStatusFields(t, s, a, d);
					
					try 
					{	
						Thread.sleep(300);
				    } 
					catch (InterruptedException e) 
					{
						System.out.println("Queue thread interrupted");
				    }
				
					e++;
					if(e==b)
			     	{
					  stop();  
					}
				}
			}
			else
			{

					int q=0;
				while(!done) {

			for(int i=0;i<10;i++)
			{
		  
		   queue.simulateStep();	    
			}
		   
		    double t = queue.getTime();
		    int s = queue.getState();
		    int a = queue.getLastArrivals();
		    int d = queue.getLastDepartures();

		    stateEvolData.add(t*1024, s);
			
		    updateStatusFields(t, s, a, d);
			 
		    try {	
			Thread.sleep(300);
		    } catch (InterruptedException e) {
			System.out.println("Queue thread interrupted");
		    }
		  q++;
		  if(q==b)
		  {
			  stop();  
		 }
		}
			}
		}
	   
	    
	    public void stop() {
			if (thread != null) {
			    thread.suspend();
		        System.out.println("Simulation suspended");
			} 
		    }
	    
	    public void reset() {
			done = true;
			stateEvolData.clear();
			updateStatusFields(0, 0, 0, 0);
			
			System.out.println("Simulation reset");
		    }
	    
	    public void destroy() {

	    	
	    	if (thread != null) {
	    	    
	        }
	    }
	public static void main(String args[])
	{
		new MarkovApplet();
	}
	public void actionPerformed(ActionEvent e) {
		    
	}
}
	